package com.ad.pet.app.login;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.result.ResultBean;

import javax.inject.Inject;

/**
 * Created by dell - on 2018/1/18.
 */
@PerActivity
public class MobileInputPresenter extends AbsBasePresenter implements MobileInputContract.Presenter {

    private ApiService mApiService;
    private MobileInputContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public MobileInputPresenter(ApiService apiService,
                                Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable!=null)
        mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void onCreate(MobileInputContract.View view) {
        this.mView = view;
    }




    @Override
    public void checkPhone(String phone) {
        mRxDisposable.add(mApiService.checkMobile( phone)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.checkPhoneSuccess(user.getIs_register());
                    }
                }));
    }

    @Override
    public void checkBindThird(String uid) {
        mRxDisposable.add(mApiService.check_bind_third(uid)
                .subscribe(new AppSubscriber<ResultBean<User>>(mView) {
                    @Override
                    public void next(ResultBean<User> bean) {
                        if (bean.getResponseData().getIs_bind_mobile()==1){
                            mView.checkBindThirdSuccess(bean.getResponseData());
                        }else {
                            mView.checkBindUnBind();
                        }
                    }
                }));
    }

    @Override
    public void thirdLogin(String email,String code,String pwd,String thirdPart,String nickName ,String headUrl,String unqionId,String to_reference_codeBODY) {

        mRxDisposable.add(mApiService.thirdparty_login(
                email, code,pwd,thirdPart,nickName,headUrl,unqionId,to_reference_codeBODY)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.thirdLoginSuccess(user);
                    }
                }));
    }
}
